package com.cjxt.ams.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cjxt.ams.dao.UserDao;
import com.cjxt.ams.domain.User;
import com.cjxt.ams.service.UserLoginService;
import com.cjxt.ams.utils.MD5Utils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author XueYunFei
 * @version V1.0
 * @Description:
 * @className: UserDaoIMpl
 * @date 2021/11/22 10:25
 * @company:华勤技术股份有限公司
 * @copyright: Copyright (c) 2021
 */


@Service
public class UserLoginServiceImpl extends ServiceImpl<UserDao, User> implements UserLoginService {

    public static long minutes;
    public static  User user;

    @Autowired
    UserDao userDao;

    @Override
    public boolean login(User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
        queryWrapper.eq("userAccount", user.getUserAccount()).eq("password", user.getPassword());
        List<User> userList = this.getBaseMapper().selectList(queryWrapper);
        if (userList != null && userList.size() > 0) {
            User temp = userList.get(0);
            BeanUtils.copyProperties(temp, user);
            return true;
        }
        return false;
    }

    /**
     * @param str
     * @description: 判断密码是否包含字母
     * @author XueYunFei
     * @date 2021/12/1 9:47
     */
    @Override
    public boolean judgeContainsStr(String str) {
        String regex = ".*[a-zA-Z]+.*";
        Matcher m = Pattern.compile(regex).matcher(str);
        return m.matches();
    }

    /**
     * @description: 修改密码
     * @author XueYunFei
     * @date 2021/12/7 14:04
     * @param useraccount 初始密码
     * @param password 第一次输入的密码
     * @param newPassword 第二次输入的密码
     */
    @Override
    public boolean updatePassword(String useraccount, String password, String newPassword) {

        User userEntity = new User();
        MD5Utils utils=new MD5Utils();
        userEntity.setPassword(utils.string2MD5(newPassword));
        return this.update(userEntity,
                new QueryWrapper<User>().eq("useraccount", useraccount).eq("password", utils.string2MD5(password)));
    }

    /**
     * @description: 判断账号是否锁定
     * @author XueYunFei
     * @date 2022/1/3 9:40
     * @param userAccount 登录用户账号
     * @return Result
     */
    @Override
    public boolean judegLock(String userAccount) {

         user = userDao.selectOne(new QueryWrapper<User>().eq("useraccount", userAccount));

        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        if (user == null || user.getLockTime() == null) {
            return false;
        }

        Date lockTime = null;//账号锁定时间
        try {
            lockTime = dateFormat.parse(user.getLockTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Date nowTime = null;//当前时间Tue Jan 04 04:48:45 CST 2022
        try {
            nowTime = dateFormat.parse(dateFormat.format(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        long diff = nowTime.getTime() - lockTime.getTime();

        long year = (diff / (24 * 60 * 60 * 1000)) / 365;//年

        long days = diff / (1000 * 60 * 60 * 24);//天数

        long hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);

        minutes = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);


        System.out.println(minutes);
        if (days < 1 && hours < 1 && minutes <= 30) {
            System.out.println("登录失败次数过多__________________");
            return true;
        }

        return false;
    }


}
